<template>
  <div>
    <avue-crud
      :option="optionInvoiceManagementList"
      :table-loading="loading"
      :data="data"
      :page="page"
      v-model="form"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @on-load="onLoad"
    >
      <template #menu="{ row, index, size }">
        <el-button type="text" icon="el-icon-view" @click="openView(row)"
          >查看</el-button
        >

        <el-button
          type="text"
          icon="el-icon-document-checked"
          @click="pay(row)"
          v-if="row.applyStatus == 0"
          >开票</el-button
        >
      </template>
      <template #menuLeft>
        <el-form :inline="true" :model="query" class="demo-form-inline">
          <el-form-item>
            <el-date-picker
              size="small"
              v-model="searchDate"
              type="daterange"
              class="m-r-10"
              value-format="yyyy-MM-dd HH:mm:ss"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              @change="conditionalQuery"
            />
          </el-form-item>
          <el-form-item label="">
            <el-select
              v-model="query.applyStatus"
              placeholder="开票状态"
              @change="conditionalQuery"
              size="small"
            >
              <el-option label="未开票" value="0"></el-option>
              <el-option label="已开票" value="1"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="">
            <el-input
              size="small"
              placeholder="请输入手机号，发票抬头"
              v-model="query.queryWord"
              @change="conditionalQuery"
            ></el-input>
          </el-form-item>
        </el-form>
      </template>
      <template #footer>
        <div class="w-50 m-t-20">
          <el-descriptions column="2" :colon="false">
            <el-descriptions-item
              label="开票总金额(元)"
              content-class-name="c-blue f-s-20 lh-1"
              >{{ totalInvoiceMoney }}</el-descriptions-item
            >
          </el-descriptions>
        </div>
      </template>
    </avue-crud>
    <el-dialog
      title="开发票"
      append-to-body
      :visible.sync="payDialog"
      width="400px"
    >
      <el-form :model="form" label-width="80px">
        <el-form-item label="发票">
          <el-upload
            class="upload-demo"
            action="https://jsonplaceholder.typicode.com/posts/"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :before-remove="beforeRemove"
            :limit="1"
            :on-exceed="handleExceed"
            :file-list="fileList"
          >
            <el-button size="small" type="text">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="备注">
          <el-input type="textarea" v-model="form.invoiceRemark"></el-input>
        </el-form-item>
      </el-form>
      <div class="dividing-line m-t-20 m-b-20"></div>
      <div class="flex-end">
        <el-button
          type="primary"
          size="small"
          :loading="adjustLoading"
          @click="doAdjust"
          >确定</el-button
        >
        <el-button type="primary" size="small" plain @click="payDialog = false"
          >取消</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { isListEmpty, msgSuccess } from "@/util/common-util";
import {
  getList,
  summary,
  doReceipt,
} from "@/api/financial/invoice-management";
import { optionInvoiceManagementList } from "@/const/financial/invoice-management";
import { mapGetters } from "vuex";

export default {
  data() {
    return {
      fileList: [],
      form: {},
      query: {
        applyStatus: "",
      },
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selectionList: [],
      optionInvoiceManagementList,
      showSettingFlag: false,
      data: [],
      searchDate: null,
      optionIsSettled: [
        {
          label: "全部",
          value: undefined,
        },
        {
          label: "未结算",
          value: 0,
        },
        {
          label: "已结算",
          value: 1,
        },
      ],
      payDialog: false,
      totalInvoiceMoney: "",
      adjustLoading: false,
    };
  },
  computed: {
    ...mapGetters(["permission"]),
  },
  methods: {
    doAdjust() {
      this.adjustLoading = true;
      doReceipt(this.form).then((res) => {
        this.adjustLoading = false;
        msgSuccess();
        this.payDialog = false;
        this.onLoad(this.page);
      });
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件`
      );
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
    pay(row) {
      this.payDialog = true;
    },
    openView(row) {
      this.$emit("openDistributionStatementDetail", row);
    },
    //账单结算
    isSetting(row) {
      this.showSettingFlag = true;
    },
    //搜索查询
    conditionalQuery() {
      if (isListEmpty(this.searchDate)) {
        this.query.startTime = "";
        this.query.endTime = "";
      } else {
        this.query.startTime = this.searchDate[0];
        this.query.endTime = this.searchDate[1];
      }
      this.onLoad(this.page, this.query);
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
      });
      summary(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then((res) => {
        this.totalInvoiceMoney = res.data.data.totalInvoiceMoney;
      });
    },
  },
};
</script>

<style lang="scss">
.lh-1 {
  line-height: 20px !important;
  padding-bottom: 50px;
}
</style>
